/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * openFuyao is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

package apiserver

import (
	"openfuyao/oauth-webhook/cmd/oauth-webhook/app/config"
	"openfuyao/oauth-webhook/pkg/httpserver"
	"testing"
)

func TestNewOAuthWebhookAPIServer(t *testing.T) {
	type args struct {
		cfg    *config.OAuthWebhookConfig
		stopCh <-chan struct{}
	}
	tests := []struct {
		name    string
		args    args
		want    string
		wantErr bool
	}{
		{
			"test http server",
			args{
				cfg:    config.NewDefaultOAuthWebhookConfig(),
				stopCh: make(<-chan struct{}),
			},
			":9095",
			false,
		},
		{
			"test https server",
			args{
				cfg: &config.OAuthWebhookConfig{
					HttpServerConfig: &httpserver.ServerOptions{
						HttpPort:          0,
						HttpsPort:         9096,
						TlsCertFile:       "",
						TlsPrivateKeyFile: "",
						MasterCAFile:      "",
					},
					JWTPrivateKey: []byte("i_am_the_secrets"),
				},
				stopCh: make(<-chan struct{}),
			},
			":9096",
			true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := NewOAuthWebhookAPIServer(tt.args.cfg, tt.args.stopCh)
			if (err != nil) != tt.wantErr {
				t.Errorf("NewOAuthWebhookAPIServer() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != nil {
				if got.Server.Addr != tt.want {
					t.Errorf("NewOAuthWebhookAPIServer() got = %v, want %v", got.Server.Addr, tt.want)
				}
			}
		})
	}
}
